package com.example.administrator.hqq.activity.dating;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.activity.LogInActivity;
import com.example.administrator.hqq.base.MyActivity;
import com.example.administrator.hqq.base.MyApplication;
import com.example.administrator.hqq.bean.dating.DatingDetailsBean;
import com.example.administrator.hqq.bean.event.DatingEvent;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.config.UrlFactory;
import com.example.administrator.hqq.utils.AlertDialogUtil;
import com.example.administrator.hqq.utils.HintWordsUtils;
import com.google.gson.Gson;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class MineDatingDetailsActivity extends MyActivity {

    @BindView(R.id.return_auto)
    AutoRelativeLayout returnAuto;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.report_auto)
    AutoRelativeLayout reportAuto;
    @BindView(R.id.search)
    TextView search;
    @BindView(R.id.issue)
    TextView issue;
    @BindView(R.id.refresh)
    AutoLinearLayout refresh;
    @BindView(R.id.compile)
    AutoLinearLayout compile;
    @BindView(R.id.delete)
    AutoLinearLayout delete;
    @BindView(R.id.introduce)
    TextView introduce;
    @BindView(R.id.pic)
    ImageView pic;
    private String TAG = this.getClass().getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_dating_details);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initview();
        initdata();
    }

    @Override
    protected String getActivityName() {
        return this.getClass().getSimpleName();
    }

    private void initdata() {
        OkHttpUtils.get().url(UrlFactory.getDatingDetail())
                .addParams(LoongsConfig.user_id, LoongsConfig.getUser_id())
                .addParams(LoongsConfig.dating_id, getIntent().getStringExtra(LoongsConfig.dating_id))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "详情角详情onError: " + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, "详情角详情onResponse: " + response);
                Gson gson = new Gson();
                DatingDetailsBean datingDetailsBean = gson.fromJson(response, DatingDetailsBean.class);
                if (datingDetailsBean.getCode()==200){
                    if (!TextUtils.isEmpty(datingDetailsBean.getDating_id().getUrl()))
                        Glide.with(MineDatingDetailsActivity.this)
                                .load(datingDetailsBean.getDating_id().getUrl())
                                .fitCenter().into(pic);
                    introduce.setText(datingDetailsBean.getDating_id().getText());
                }
            }
        });
    }

    private void initview() {
        title.setText("我的交友贴");
    }

    @OnClick({R.id.return_auto, R.id.refresh, R.id.compile, R.id.delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.return_auto:
                finish();
                break;
            case R.id.refresh:
                refreshDating();
                break;
            case R.id.compile:
                CreatDatingActivity.start(this,getIntent().getStringExtra(LoongsConfig.dating_id),CreatDatingActivity.Type.COMPILE);
                break;
            case R.id.delete:
                AlertDialogUtil.showTitleAlertDialog(this, "确定要删除此贴子吗","确定","取消", new AlertDialogUtil.AlertDialogClick.ConfirmClick() {
                    @Override
                    public void confirm() {
                        deleteDating();
                    }
                });
                break;
        }
    }

    private void deleteDating() {
        OkHttpUtils.get().url(UrlFactory.deleteDating())
                .addParams(LoongsConfig.user_id,LoongsConfig.getUser_id())
                .addParams(LoongsConfig.dating_id,getIntent().getStringExtra(LoongsConfig.dating_id))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "onError: "+e.getMessage() );
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, "onResponse: "+response );
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int code = jsonObject.getInt(LoongsConfig.code);
                    if (code==200){

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        DatingEvent datingEvent = new DatingEvent(DatingEvent.DatingType.DELETE);
        datingEvent.setDating_id(getIntent().getStringExtra(LoongsConfig.dating_id));
        EventBus.getDefault().post(datingEvent);
        finish();
    }

    /**
     * 刷新置顶
     */
    private void refreshDating() {
        OkHttpUtils.get().url(UrlFactory.setTop())
                .addParams(LoongsConfig.dating_id,getIntent().getStringExtra(LoongsConfig.dating_id))
                .addParams(LoongsConfig.user_id,LoongsConfig.getUser_id())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "刷新置顶onError: "+e.getMessage() );
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, "刷新置顶onResponse: "+response );
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int code = jsonObject.getInt(LoongsConfig.code);
                    String description = jsonObject.getString(LoongsConfig.description);
                    HintWordsUtils.showMarkeWords(code,description);
                    if (code==200){
                        ToastUtils.showShort("置顶成功");
                        DatingEvent datingEvent = new DatingEvent(DatingEvent.DatingType.REFRESH);
                        EventBus.getDefault().post(datingEvent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(DatingEvent datingEvent){
        switch (datingEvent.getDatingType()) {
            case REFRESH:
                break;
            case CREAD:
                break;
            case DELETE:
                break;
            case COMPILE:
                initdata();
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
